import { defineStore } from 'pinia'
import ls from '@/utils/securels'
import store from '../index'
import router from '@/router'

interface state {
  theme: g.theme
  color: g.color
  from1: string
  from2: string
  token: string
  orgId: number | ''
  platformId: number | ''
  components: any
}

export const userStore = defineStore('user', {
  state: (): state => ({
    token: '',
    theme: 'defaultAlgorithm',
    // from1: 'http://192.168.100.50:8081/',
    from1: window.location.protocol + '//' + window.location.hostname + ':8081/',
    from2: 'gateway',
    orgId: '',
    platformId: 102,
    components: {
      Button: {
        colorPrimary: '#165DFF',
        colorBorder: '#C9CDD4',
        colorPrimaryHover: '#4080FF',
        colorText: '#4E5969',
        colorLink: '#165DFF',
        colorLinkHover: '#4080FF'
      },
      Tabs: {
        colorPrimary: '#165DFF',
        colorText: '#4E5969',
        colorPrimaryHover: '#4080FF'
      },
      Switch: {
        colorPrimary: '#165DFF',
        colorPrimaryHover: '#4080FF'
      },
      Radio: {
        colorPrimary: '#165DFF'
      },
      Pagination: {
        colorPrimary: '#165DFF',
        colorPrimaryHover: '#4080FF'
      },
      Checkbox: {
        colorPrimary: '#165DFF',
        colorPrimaryHover: '#4080FF'
      },
      Select: {
        colorPrimaryBg: '#E8F3FF'
      }
    },
    color: {
      colorPrimary: '#165DFF',
      colorDanger: '#f53f3f',
      borderRadius: 2,
      colorPrimaryHover: '#4080FF',
      colorPrimaryBg: '#E8F3FF'
    }
  }),
  getters: {},
  actions: {
    changeTheme(payload: g.theme) {
      this.theme = payload
    },
    changeThemeColor(payload: g.color) {
      this.color = payload
    },
    setToken(payload: string) {
      this.token = payload
    },
    LoginOut() {
      store.userApp.userInfo = ''
      this.token = ''
      router.push('/Login')
    }
  },
  persist: {
    storage: {
      setItem: (key, value) => ls.set(key, value),
      getItem: (key) => ls.get(key)
    }
  }
})
